const axios = require('axios')
const createNodeHelpers = require('gatsby-node-helpers').default
const { paginate } = require("gatsby-awesome-pagination")

exports.sourceNodes = async ({ actions }, { apiUrl }) => {
  const { createNode } = actions
  const { data: { data = [] } } = await axios.get(`${apiUrl}/users`)
  const { createNodeFactory } = createNodeHelpers({ typePrefix: 'userListData' })
  // 创建节点对象到数据层
  const createNodeObject = createNodeFactory('listInfo')
  data.forEach(user => {
    createNode(createNodeObject(user))
  })
}
exports.createPages = async ({ actions, graphql }) => {
  const { createPage } = actions
  const { data } = await graphql(`
    query {
      allUserListDataListInfo {
        nodes {
          id
        }
      }
    }
  `)
  paginate({
    createPage, // The Gatsby `createPage` function
    items: data.allUserListDataListInfo.nodes, // An array of objects
    itemsPerPage: 10, // How many items you want per page
    pathPrefix: "/paginate", // Creates pages like `/blog`, `/blog/2`, etc
    component: require.resolve("../../src/templates/paginate.js"), // Just like `createPage()`
  })
}
